<template>
  <div class="msgArea">
    <div class="areaTitle">基本信息</div>
    <div class="areaList">
      <div class="listItem">
        <div class="title">申请人姓名</div>
        <div class="value">{{ previewMsg.custName }}</div>
      </div>
      <div class="listItem">
        <div class="title">身份证</div>
        <div class="value">{{ previewMsg.cardNo }}</div>
      </div>
      <div class="listItem">
        <div class="title">性别</div>
        <div class="value">{{ previewMsg.sex }}</div>
      </div>
      <div class="listItem">
        <div class="title">民族</div>
        <div class="value">{{ previewMsg.nation }}</div>
      </div>
      <div class="listItem">
        <div class="title">出生年月日</div>
        <div class="value">{{ previewMsg.birth }}</div>
      </div>
      <div class="adress">
        <div class="title">地址</div>
        <div class="value">{{ previewMsg.address }}</div>
      </div>
      <div class="listItem">
        <div class="title">签发机关</div>
        <div class="value">{{ previewMsg.authority }}</div>
      </div>
      <div class="listItem">
        <div class="title">有效日期</div>
        <div class="value">{{ previewMsg.validDate }}</div>
      </div>
      <div class="listItem">
        <div class="title">手机号</div>
        <div class="value">{{ previewMsg.custPhone }}</div>
      </div>
    </div>
  </div>
  <div class="msgArea" v-if="!isCommon">
    <div class="areaTitle">贷款办理信息</div>
    <div class="areaList">
      <div class="listItem">
        <div class="title">区域</div>
        <div class="value">合肥</div>
      </div>
      <div class="listItem">
        <div class="title">办理机构</div>
        <div class="value">{{ custodianOrgName }}</div>
      </div>
      <div class="listItem">
        <div class="title">专属客户经理</div>
        <div class="value">王珊珊</div>
      </div>
    </div>
  </div>
  <div class="msgArea" v-if="isCommon">
    <div class="areaTitle">贷款办理信息(选填)</div>
    <div class="areaList">
      <div class="listItem" @click="isArea = true">
        <div class="title">区域</div>
        <div class="value">
          <div>合肥</div>
          <div class="pickIcon"></div>
        </div>
      </div>
      <div class="listItem" @click="goCheckOrg">
        <div class="title">办理机构</div>
        <div class="value">
          <div>{{ custodianOrgName }}</div>
          <div class="pickIcon"></div>
        </div>
      </div>
    </div>
  </div>
  <div class="btnArea">
    <div class="btn lastStep" @click="lastStep">上一步</div>
    <div class="btn submit" @click="submit">提交</div>
  </div>

  <!-- 公码--区域 -->
  <up-picker
    :show="isArea"
    :columns="areaColumns"
    :defaultIndex="[0]"
    @cancel="isArea = false"
    @confirm="checkArea"
  ></up-picker>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useAppStore } from "../../store/index";
import { onLoad, onShow } from "@dcloudio/uni-app";
const store = useAppStore();
import * as http from "../../utils/http";
import config from "../../utils/config";
// 公码/私码
const isCommon = config.source == "1" ? ref(true) : ref(false);

const previewMsg = reactive({});
let param = null;
const custodianOrgName = ref("");
const custodianOrg = ref("");
onLoad((options) => {
  param = JSON.parse(options.value);
  let {
    custName,
    cardNo,
    sex,
    nation,
    birth,
    address,
    authority,
    validDate,
    custPhone,
  } = param;
  previewMsg.custName = custName;
  previewMsg.cardNo = cardNo;
  previewMsg.sex = sex;
  previewMsg.nation = nation;
  previewMsg.birth = birth;
  previewMsg.address = address;
  previewMsg.authority = authority;
  previewMsg.validDate = validDate;
  previewMsg.custPhone = custPhone;
});
onShow(() => {
  if (store.orgId) {
    custodianOrg.value = store.orgId;
    custodianOrgName.value = store.orgShortName;
  } else {
    http
      .orgChoose({
        belongRegion: 1,
      })
      .then((res) => {
        custodianOrg.value = res.data[0].orgId;
        store.setOrgId(res.data[0].orgId);
        custodianOrgName.value = res.data[0].orgShortName;
        store.setOrgShortName(res.data[0].orgShortName);
        wx.setStorageSync("orgId", res.data[0].orgId);
        wx.setStorageSync("orgShortName", res.data[0].orgShortName);
        custodianOrg.value = res.data[0].orgId;
        // 暂时
        custodianOrg.value = "3401010193";
        store.setOrgId("3401010193");
        custodianOrgName.value = "滨湖支行";
        store.setOrgShortName("滨湖支行");
        wx.setStorageSync("orgId", "3401010193");
        wx.setStorageSync("orgShortName", "滨湖支行");
      });
  }
});

// 公码--区域
const isArea = ref(false);
const areaIndex = ref(1);
const areaColumns = reactive([["合肥", "铜陵", "宿州", "蚌埠", "六安"]]);
const checkArea = (e) => {
  // 业务代码
  http
    .orgChoose({
      belongRegion: e.indexs[0] + 1,
    })
    .then((res) => {
      areaIndex.value = e.indexs[0] + 1;
      custodianOrg.value = res.data[0].orgId;
      store.setOrgId(res.data[0].orgId);
      custodianOrgName.value = res.data[0].orgShortName;
      store.setOrgShortName(res.data[0].orgShortName);
    });

  // 选择器关闭
  isArea.value = false;
};

const goCheckOrg = () => {
  wx.navigateTo({
    url: "../checkAdress/checkAdress?value=" + JSON.stringify(areaIndex.value),
  });
};

// 上一步
const lastStep = () => {
  wx.navigateBack();
};
// 提交
const submit = () => {
  let fileMap = {};
  fileMap[store.cardNeFileId] = store.cardNeFileName;
  fileMap[store.cardPrFileId] = store.cardPrFileName;
  http
    .creditApply({
      creditFileId: store.creditFileId,
      sensitiveFileId: store.sensitiveFileId,
      creditContractId: store.creditContractId,
      sensitiveContractId: store.sensitiveContractId,
      orderNo: store.orderNo,
      // 机构的码值
      custodianOrg: custodianOrg.value,
      fileMap,
      ...param,
    })
    .then((res) => {
      http.allocationCt({
        orderId: store.orderId,
      });
      wx.showLoading({
        title: "授信结果查询中",
        mask: true,
      });
      // #拿到授信id
      setTimeout(() => {
        wx.hideLoading();
        http
          .creditResult({
            playersId: param.bizId,
            orderId: store.orderId,
          })
          .then((res) => {
            let param = {};
            // 是否有预授信额度判断是否返回
            if (res.data && res.data.finaLoanAmount) {
              param.finaLoanAmount = res.data.finaLoanAmount;
              param.isgetResult = true;
            } else {
              param.isgetResult = false;
            }
            wx.reLaunch({
              url:
                "../submitApplicate/submitApplicate?value=" +
                JSON.stringify(param),
            });
          })
          .catch((err) => {
            let param = {};
            param.isgetResult = false;
            wx.reLaunch({
              url:
                "../submitApplicate/submitApplicate?value=" +
                JSON.stringify(param),
            });
          });
      }, 3000); //3s后调风控查询结果
    });
};
</script>

<style scoped lang="scss">
.msgArea {
  .areaTitle {
    height: 59rpx;
    line-height: 59rpx;
    padding: 0 20rpx;
    background-color: #f8f8f8;
  }
  .areaList {
    padding: 0 35rpx;
    .listItem {
      height: 84rpx;
      line-height: 84rpx;
      padding: 0 5rpx;
      display: flex;
      justify-content: space-between;
      border-bottom: 1rpx solid #eeeeee;
    }
    .title {
      font-size: 28rpx;
    }
    .value {
      font-size: 28rpx;
      color: #999999;
      display: flex;
      align-items: center;
      .pickIcon {
        width: 12rpx;
        height: 12rpx;
        border-bottom: 2rpx solid #999999;
        border-right: 2rpx solid #999999;
        transform: rotate(-45deg);
        margin-left: 37rpx;
      }
    }
    .adress {
      border-bottom: 1rpx solid #eeeeee;
    }
  }
}
.btnArea {
  display: flex;
  justify-content: space-between;
  padding: 53rpx 40rpx;
  .btn {
    width: 300rpx;
    height: 72rpx;
    line-height: 72rpx;
    border-radius: 36rpx;
    text-align: center;
    font-size: 32rpx;
  }
  .lastStep {
    color: #406eb0;
    border: 2rpx solid #74a8f2;
  }
  .submit {
    background-image: linear-gradient(to right, #619df1, #4478c1);
    color: #fff;
    box-shadow: 0 4rpx 12rpx 0 #a4c3f3;
  }
}
</style>
